from django.shortcuts import render
from clusterer.kmeans import Kmeans
from clusterer.dbscan import Dbscan
from clusterer.birch import Birch
from BigDataWeb.view import get_algorithm
from BigDataWeb.view import put_algorithm
from BigDataWeb.view import read_data_source


def kmeans(request):
    context = {}
    algorithm = Kmeans()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "clusterer/1.html", context)


def dbscan(request):
    context = {}
    algorithm = Dbscan()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "clusterer/1.html", context)


def birch(request):
    context = {}
    algorithm = Birch()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "clusterer/1.html", context)


def select_data_source(request):
    context = {}
    algorithm = get_algorithm(request)
    read_data_source(request, algorithm)
    context["algorithm"] = algorithm
    return render(request, "clusterer/2.html", context)


def understand_data_set(request):
    context = {}
    algorithm = get_algorithm(request)
    algorithm.setInPutFieldName(request.POST.getlist("input_field_names"))
    context["algorithm"] = algorithm
    return render(request, "clusterer/3.html", context)


def configure_parameters(request):
    context = {}
    algorithm = get_algorithm(request)
    
    if algorithm.algorithm_name == "K均值":
        algorithm.k_value = int(request.POST.get("k_value"))
        
    if algorithm.algorithm_name == "DBSCAN":
        algorithm.eps = float(request.POST.get("eps"))
        algorithm.min_samples = int(request.POST.get("min_samples"))
        
    if algorithm.algorithm_name == "BIRCH":
        n_clusters = request.POST.get("n_clusters")
        if n_clusters:
            algorithm.n_clusters = int(n_clusters)
        else:
            algorithm.n_clusters = None
    
    algorithm.implent()
    algorithm.generateNotebookFile()

    context["algorithm"] = algorithm
    return render(request, "clusterer/5.html", context)
